
const app = getApp();
var http = require('../../../../service/http.js');//请求类
var util = require('../../../../utils/util.js');//工具类
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //输入框操作
    searchVal:'',
    searchHistory:[],//搜索历史
    searchList:[],//搜索出来的商品列表
    total:0,//分页总数
    pageNo:1,//当前页数
    second:0,//搜索计时
    timeout:'',//停止计时
     pageUrl:'',//跳转url
     
    shopuid: '',//店铺id
    shopName: '',//店铺名称
    isOwnShop: '',//是否是自己有分销店铺
    //加载更多
    showLoadding: false,//加载动画gif
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let shopInfo = wx.getStorageSync('shopInfo');
    let that = this;
    let searchHistory = wx.getStorageSync('searchHistory');
    if (searchHistory){
      that.setData({
        searchHistory: searchHistory,
        shopuid: shopInfo.shopuid || ''
      })
    } 
  },
  
  //查询搜索列表
  searchListFun:function(){
    console.log('searchList根据输入搜索商品')
    let [that, searchVal, pageNo, pageSize, second] = [this, this.data.searchVal, 1, app.data.pageSize, this.data.second];
    console.log('查询内容=' + searchVal);
    let shopInfo = wx.getStorageSync('shopInfo');
    let shopuid = '';
    if (shopInfo.shopuid != undefined && shopInfo.shopuid != null && shopInfo.shopuid != '') {
      shopuid = shopInfo.shopuid;
    }
    if (searchVal) {
        console.log('搜索请求');
        if (second == 2){
          that.setData({
            showLoadding:true
          })
          http.getReq('/weixin/item/itemList', {
            itemName: searchVal,
            pageNo: pageNo,
            pageSize: pageSize,
            shopuid: shopuid, //用于根据店铺id查找该店铺上架的商品
            cityName: app.data.cityName || '北京',//获取地理位置
          }, function (res) {
            console.log(res);
            let searchList = res.data;
            
            let searchHistory = wx.getStorageSync('searchHistory');//本地历史搜索
            
            //获取商品最大最小价格
            if (searchList.length > 0){
              
              app.getNewList(searchList);
              searchList = app.data.itemList
              
              that.saveSearchVal(searchVal);//有搜索结果 将搜索值存入本地历史搜索
            }
            
            console.log(app.data.itemList);
            that.setData({
              searchList: searchList,
              total: res.total,
              pageNo: pageNo,
              showLoadding: false
            })
          });
    
        }else{
          console.log('累计秒数小于2不发送请求');
        }

      }else{//搜索内容为空
        //初始化商品列表
        that.setData({
          searchList: [],
          total:0,
          pageNo:1,
          searchVal: searchVal
        })
      }
  },
  //存储搜索历史
  saveSearchVal: function (searchVal){
    let [that, searchHistory, localSearch] = [this, wx.getStorageSync('searchHistory'), []]//本地历史搜索
    if (searchHistory) {//有搜索历史
      searchHistory.reverse();//将倒叙变为正序
      //判断搜索内容是否包含在搜索历史中
      if (searchHistory.indexOf(searchVal) > -1) {//包含
        for (let s = 0; s < searchHistory.length; s++) {
          if (searchHistory[s] == searchVal) {
            searchHistory.splice(s, 1);//移除该元素
          }
        }
      }
      searchHistory.push(searchVal)//将搜索值放在首位

      searchHistory.reverse();//倒叙排列

      if (searchHistory.length > 2) {//列表长度为2，多余删除
        searchHistory.pop();
      }
      that.setData({
        searchHistory: searchHistory
      })

      wx.setStorageSync('searchHistory', searchHistory);
    } else {//无搜索历史
      localSearch.push(searchVal);
      wx.setStorageSync('searchHistory', localSearch);
    }
  
  },
  //两次查询之间秒数计时计时
  timeCount: function () {
    let [that, second, t] = [this, this.data.second, ''];

    second = second + 1

    let timeout = that.data.timeout;
    that.setData({
      second: second
    })
    console.log('second----' + second);
    let tt;
    if (second >= 2) {
      //设置累计秒数
      clearTimeout(setTimeout(function () {
        that.timeCount()
      }, 1000));
      that.searchListFun();
    } else {
      // that.setData({
      timeout: setTimeout(function () {
        that.timeCount()
      }, 1000)
      //})
    }

  },
  //初始化秒数
  startTime: function (e) {
    console.log('初始化秒数');
    this.setData({
      second: 0,
      searchVal: e.detail.value//当前搜索内容
    })
    this.timeCount();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('显示平台店铺信息');
    this.setData({
      pageUrl:''
    })
  },

  //取消搜索
  cancelSearch:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  //删除搜索记录
  delSearchHistory:function(e){
    console.log("删除搜索记录");
    console.log(e);
    let that = this;
    let index = e.currentTarget.dataset.index;
    //   wx.getStorageSync('searchHistory');
    let searchHistory = wx.getStorageSync('searchHistory');
    searchHistory.splice(index,1);//移除数组；
    that.setData({
      searchHistory: searchHistory
    })
    wx.setStorageSync('searchHistory', searchHistory);//本地搜索记录
  },
  //加载更多
  onReachBottom: function (e) {
    console.log('加载更多');
    let [pageNo, total, that, searchVal, searchList] = [this.data.pageNo, this.data.total, this, this.data.searchVal, this.data.searchList];

    if (pageNo < total){
      pageNo = pageNo + 1;
      that.itemList(searchVal, pageNo, searchList);
    }
  },
  //根据历史记录搜索商品列表
  searchByHistory:function(e){
    console.log(e);
    let [searchVal, pageNo, searchList] = [e.currentTarget.dataset.val,1,[]];
    this.itemList(searchVal, pageNo, searchList);
    //改变搜索历史位置
    let searchHistory = wx.getStorageSync('searchHistory');
    for (let s = 0; s < searchHistory.length; s++){
      if (searchHistory[s] == searchVal ){
        searchHistory.splice(s,1);//移除该元素
        searchHistory.push(searchVal);
      }
    } 
    searchHistory.reverse();
    console.log(searchHistory);
    wx.setStorageSync('searchHistory', searchHistory);

  },

  itemList: function (searchVal, pageNo, searchList){//根据搜索内容搜索商品列表
    let that = this;
    //显示加载
    that.setData({
      showLoadding: true,//加载动画gif
    })
    let shopInfo = wx.getStorageSync('shopInfo');
    let shopuid = '';
    if (shopInfo.shopuid != undefined && shopInfo.shopuid != null && shopInfo.shopuid != '') {
      shopuid = shopInfo.shopuid;
    }

    http.getReq('/weixin/item/itemList', {
      itemName: searchVal,
      pageNo: pageNo,
      pageSize: app.data.pageSize,
      shopuid: shopuid, //用于根据店铺id查找该店铺上架的商品
      cityName: app.data.cityName || '北京',//获取地理位置
    }, function (res) {
     
      let newList = res.data;//返回分页商品
      console.log(res);
      let proList = searchList || [];

      if (newList.length > 0){
        app.getNewList(newList);//获取商品最大最小价格
        proList.push(...app.data.itemList);//在原数组基础上进行拼接
      }

      //返回符合条件的列表
      that.setData({
        searchList: proList,
        total: res.total,
        pageNo: pageNo,
        searchVal: searchVal,
        showLoadding: false,//加载动画gif
      })
      
    });

  },
  selectProductById: function (e) {//根据商品id 查商品详情
    console.log('根据商品id 查商品详情参数');
    console.log(e);

    this.setData({
      productId: e.currentTarget.dataset.productid,
      pageUrl: '/pages/product/product'
    })
    app.selectProductById(this)
  },
  //------------------未开通服务提示----------------------
  unOpen: function () {
    app.unOpen();
  },
  //商品列表缩略图 图片加载失败出现错误时替换图片路径
  errImg: function (e) {
    console.log(e);
    let proIndex = e.currentTarget.dataset.proindex;//商品下标
    let isSellHot = e.currentTarget.dataset.isSellHot;//是否是热销列表
    let searchList = this.data.searchList;
    this.setData({
      searchList: app.errImg(searchList, proIndex)//返回products 更改thumbImgs路径
    })
  },
  onPullDownRefresh:function(){
    wx.stopPullDownRefresh();
  }
})